/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.stock;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryWarningRecord;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.SystemService;
import com.xbang.attendance.service.WarehouseService;
import com.xbang.attendance.utils.FieldUtil;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.struts.support.BaseActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class StockManager extends BaseActionSupport {

	private static final long serialVersionUID = -7802000581845975181L;

	private final static Log logger = LogFactory.getLog(StockManager.class);

	@Autowired
	private InventoryService inventoryService;

	@Autowired
	private SystemService systemService;
	/** 库存 */
	private Inventory inventory;

	/** 仓储 */
	private List<Warehouse> chooseWarehouse = new ArrayList<Warehouse>();

	private List<Product> chooseProduct = new ArrayList<Product>();

	private List<Integer> warehouseIds = new ArrayList<Integer>();

	private List<Integer> productIds = new ArrayList<Integer>();

	private InventoryWarningRecord inventoryWarningRecord;

	private Integer id;

	private String[] fields;

	private Map<String, String> fieldsMap;
	private Date beginDate;
	private Date endDate;
	@Autowired
	private WarehouseService warehouseService;

	@Action("/warehouse/stock/order")
	public String order() throws Exception {
		return "order";
	}

	@Action("/warehouse/stock/add")
	public String add() throws Exception {
		getMoreInfo();
		return "edit";
	}

	@Action("/warehouse/warning/${id}/view")
	public String viewWarning() throws Exception {
		if (inventoryWarningRecord == null && id != null) {
			inventoryWarningRecord = (InventoryWarningRecord) systemService.getObject(InventoryWarningRecord.class, id);
			inventoryWarningRecord.setRaedWarning(true);
			systemService.update(inventoryWarningRecord);
		}
		return "warning";
	}

	@Action("/warehouse/stock/${id}/edit")
	public String edit() throws Exception {
		if (inventory == null && id != null) {
			inventory = (Inventory) systemService.getObject(Inventory.class, id);
		}
		getMoreInfo();
		return "edit";
	}

	@Action("/warehouse/stock/${id}/view")
	public String view() throws Exception {
		if (inventory == null && id != null) {
			inventory = (Inventory) systemService.getObject(Inventory.class, id);
		}
		return "edit";
	}

	@Action("/warehouse/stock/save")
	public String save() throws Exception {
		// /save.
//		if (inventory != null) {
//			inventoryService.inExchangeInventory(inventory.getWarehouse(), inventory.getProduct(), inventory.getAmount());
//		}
		return "r:/warehouse/stock";
	}

	private void getMoreInfo() throws Exception {
		chooseProduct = systemService.getObjects(Product.class);
		chooseWarehouse = systemService.getObjects(Warehouse.class);

	}

	@Action("/warehouse/stock/toexport")
	public String toExport() {
		//setFieldsMap(FieldUtil.stockFields);
		return "field";
	}

	@Action("/warehouse/stock/export")
	public String export() throws Exception {
		String webPath = ServletActionContext.getRequest().getRealPath("//download//warehouse//stock");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		String exportFile = contextURL ;
		try {
			exportFile += "/download/warehouse/stock/" + warehouseService.genericExcelFile(webPath,id,beginDate,endDate);	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return "r:" + exportFile;
		// return "index";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the inventory
	 */
	public Inventory getInventory() {
		return inventory;
	}

	/**
	 * @param inventory
	 *            the inventory to set
	 */
	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}

	/**
	 * @return the chooseWarehouse
	 */
	public List<Warehouse> getChooseWarehouse() {
		return chooseWarehouse;
	}

	/**
	 * @param chooseWarehouse
	 *            the chooseWarehouse to set
	 */
	public void setChooseWarehouse(List<Warehouse> chooseWarehouse) {
		this.chooseWarehouse = chooseWarehouse;
	}

	/**
	 * @return the chooseProduct
	 */
	public List<Product> getChooseProduct() {
		return chooseProduct;
	}

	/**
	 * @param chooseProduct
	 *            the chooseProduct to set
	 */
	public void setChooseProduct(List<Product> chooseProduct) {
		this.chooseProduct = chooseProduct;
	}

	/**
	 * @return the warehouseIds
	 */
	public List<Integer> getWarehouseIds() {
		return warehouseIds;
	}

	/**
	 * @param warehouseIds
	 *            the warehouseIds to set
	 */
	public void setWarehouseIds(List<Integer> warehouseIds) {
		this.warehouseIds = warehouseIds;
	}

	/**
	 * @return the productIds
	 */
	public List<Integer> getProductIds() {
		return productIds;
	}

	/**
	 * @param productIds
	 *            the productIds to set
	 */
	public void setProductIds(List<Integer> productIds) {
		this.productIds = productIds;
	}

	/**
	 * @param systemService
	 *            the systemService to set
	 */
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	/**
	 * @param inventoryService
	 */
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public String[] getFields() {
		return fields;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}

	public Map<String, String> getFieldsMap() {
		return fieldsMap;
	}

	public void setFieldsMap(Map<String, String> fieldsMap) {
		this.fieldsMap = fieldsMap;
	}

	/**
	 * @return the inventoryWarningRecord
	 */
	public InventoryWarningRecord getInventoryWarningRecord() {
		return inventoryWarningRecord;
	}

	/**
	 * @param inventoryWarningRecord
	 *            the inventoryWarningRecord to set
	 */
	public void setInventoryWarningRecord(InventoryWarningRecord inventoryWarningRecord) {
		this.inventoryWarningRecord = inventoryWarningRecord;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
}
